<?php
class ServicesController extends AppController {

	var $name = 'Services';
	var $uses = array('Service','Promotion','Tinformation','Precord','Travel','Recreation');
	
	function beforeFilter() {
    parent::BeforeFilter();   	
	//$this->Auth->allow('login','logout','userlist','add','confirmaccount');
	//$this->Auth->allow('*');
}

	function index($id=null) {
		/* $this->Service->recursive = 0;
		$this->paginate = array(
        'conditions' => array('Service.user_id' => $this->Auth->user('id')),
        'limit' => 10
    ); */
if($id==null)
		$id=$this->Auth->user('id');
		
		$this->set('services', $this->Service->findAllByUserId($id));
		
		/////////////////promotion////////////////
		/* $this->Promotion->recursive = -1;
		$this->paginate = array(
        'conditions' => array('Promotion.user_id' => $this->Auth->user('id')),
        'limit' => 10
    ); */
		$this->set('promotions', $this->Promotion->findAllByUserId($id));
		//////////////////////////////

              /*  $con=array('conditions' => array('Tinformation.user_id' => $id),
	'group' => array('Tinformation.flag')
	);*/
		$this->set('tinformations', $this->Tinformation->find('all',array('conditions'
                    => array('Tinformation.user_id' => $id),
	'order' => array('Tinformation.flag DESC')
	)));
		///////////////////////////
		$this->set('precords', $this->Precord->findAllByUserId($id));
		////////////////////////////
		$this->set('travels', $this->Travel->findAllByUserId($id));
		////////////////////////////
		$this->set('recreations',  $this->Recreation->findAllByUserId($id));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid service', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('service', $this->Service->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
		$this->data['Service']['user_id']=$this->Auth->user('id');
			$this->Service->create();
			if ($this->Service->save($this->data)) {
				$this->Session->setFlash(__('The service has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The service could not be saved. Please, try again.', true));
			}
		}
		$users = $this->Service->User->find('list');
		$this->set(compact('users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid service', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Service->save($this->data)) {
				$this->Session->setFlash(__('The service has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The service could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Service->read(null, $id);
		}
		$users = $this->Service->User->find('list');
		$this->set(compact('users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for service', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Service->delete($id)) {
			$this->Session->setFlash(__('Service deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Service was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>